package com.appspot.abcensys.server;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.appspot.abcensys.shared.Afwezigheid;
import com.appspot.abcensys.shared.Gebruiker;
import com.appspot.abcensys.shared.Les;

/**
 * Klasse waarmee diverse email berichten mee verstuurd kunnen worden
 * 
 * @author Hugo Zonderland
 */
public class MailServer {

	// InternetAdress
	private InternetAddress recipient;
	
	// RecipientType
	private RecipientType recipientType;
	
	// InternetAddress
	private InternetAddress sender;
	
	// Subject
	private String subject;
	
	// Message
	private String message;
	
	// Logger
	private static final Logger log = Logger.getLogger(MailServer.class.getName());

	/**
	 * Constructor
	 */
	public MailServer() {
		this.setSender("hugo.zonderland@gmail.com", "Abcensys");
		this.setRecipientType(Message.RecipientType.TO);
	}

	/**
	 * Stuurt een email
	 */
	private void sendMail() {

		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);

		try {

			Message msg = new MimeMessage(session);
			msg.setFrom(this.getSender());
			msg.addRecipient(this.getRecipientType(), this.getRecipient());
			msg.setSubject(this.getSubject());
			msg.setText(this.getMessage());
			Transport.send(msg);
			
			log.info("sendMail() naar :" + msg.getAllRecipients() + " over: "  + msg.getSubject()); 
			
		} catch (AddressException e) {
			log.info("sendMail() | AddressException: " + e.getMessage());
		} catch (MessagingException e) {
			log.info("sendMail() | MessagingException: " + e.getMessage());
		}
	}

	/**
	 * Stuurt een bericht naar de docent wanneer een student zich ziek meld
	 * 
	 * @param Gebruiker
	 *            gDocent docent Gebruiker
	 * @param Gebruiker
	 *            gStudent student Gebruiker
	 * @param Les
	 *            de Les waar Afwezigheid voor gemeld word
	 * @param Afwezigheid
	 *            de Afwezigheid die gemeld is
	 */
	public void sendAfmeldEmail(Gebruiker gDocent, Gebruiker gStudent, Les les, Afwezigheid afwezigheid) {

		String docentnaam = gDocent.getVoornaam() + " " + gDocent.getAchternaam();

		this.setRecipient("hugo.zonderland@gmail.com", docentnaam);
		this.setSubject("Afmelding voor les " + les.getVakCode());

		String bericht = "Beste " + docentnaam + ",";
		bericht += "\n\n";
		bericht += "Student " + gStudent.getVoornaam() + " " + gStudent.getAchternaam() + " (studentnummer " + gStudent.getStudentNummer() + ") heeft zich zojuist afgemeld voor uw les "
				+ les.getVakCode() + " op " + les.getLesDatum() + " om " + les.getStartTijd() + ".";
		bericht += "\n";
		bericht += "Reden: " + afwezigheid.getVerzuimCategorie();
		bericht += "\n";
		bericht += "Toelichting: " + afwezigheid.getToelichting();
		bericht += "\n\n";
		bericht += "U kunt deze informatie ook terugvinden in Abcensys";
		bericht += "\n\n";
		bericht += "- Abcensys";
		
		this.setMessage(bericht);
		this.sendMail();
	}

	/**
	 * @return the to
	 */
	private InternetAddress getRecipient() {
		return recipient;
	}

	/**
	 * Sets the Recipient
	 * 
	 * @param String
	 *            with the email of the recipient
	 * @param String
	 *            with the name of the recipient
	 */
	private void setRecipient(String email, String name) {
		if (email != null || name != null) {
			try {
				this.recipient = new InternetAddress(email, name);
			} catch (UnsupportedEncodingException e) {
				log.info("setRecipient() | UnsupportedEncodingException: " + e.getMessage());
			}
		}
	}

	/**
	 * @return the recipientType
	 */
	private RecipientType getRecipientType() {
		return recipientType;
	}

	/**
	 * @param recipientType
	 *            the recipientType to set
	 */
	private void setRecipientType(RecipientType recipientType) {
		if (recipientType != null) {
			this.recipientType = recipientType;
		}
		this.recipientType = Message.RecipientType.TO;
	}

	/**
	 * @return the from
	 */
	private InternetAddress getSender() {
		return sender;
	}

	/**
	 * Sets the sender
	 * 
	 * @param String
	 *            with the email of the sender
	 * @param String
	 *            with the name of the Sender
	 */
	private void setSender(String email, String name) {
		if (email == null || name == null) {
			email = "hugo.zonderland@gmail.com";
			name = "Abcensys";
		}

		try {
			this.recipient = new InternetAddress(email, name);
		} catch (UnsupportedEncodingException e) {
			log.info("setSender() | UnsupportedEncodingException: " + e.getMessage());
		}
	}

	/**
	 * @return the subject
	 */
	private String getSubject() {
		return subject;
	}

	/**
	 * @param subject
	 *            the subject to set
	 */
	private void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * @return the message
	 */
	private String getMessage() {
		return message;
	}

	/**
	 * @param message
	 *            the message to set
	 */
	private void setMessage(String message) {
		this.message = message;
	}
}
